{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatic Data Access Management \n",
    "   In this tutorial we'll explore how to use admin role to set and deploy automatic rules to accept / deny data access requests from remote users."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 - Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from syft.grid.grid_client import connect\n",
    "from syft.grid.connections.http_connection import HTTPConnection\n",
    "from syft.core.node.domain.client import DomainClient\n",
    "\n",
    "import syft as sy\n",
    "import torch as th\n",
    "\n",
    "sy.VERBOSE = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 - Connect/Authenticate with the Domain server (Using an admin account)\n",
    "We need to sign up as a Domain Admin to be able to deploy new automatic request handlers "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = connect(\n",
    "    url=\"http://localhost:5000\", # Domain Address\n",
    "    credentials= {\"email\": \"admin@email.com\", \"password\": \"pwd123\"}, # Admin role\n",
    "    conn_type= HTTPConnection, # HTTP Connection Protocol\n",
    "    client_type=DomainClient) # Domain Client type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 - Let's create a new dataset sample\n",
    "To show how it work, we'll create a random dataset sample and host it into the domain server"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age_data = th.rand((9216, 96)) # Create a new / random dataset sample\n",
    "\n",
    "# Let's tag it as \"age data\"\n",
    "age_data = age_data.tag(\"#age_data\")\n",
    "\n",
    "# Let's add a description to the tensor\n",
    "age_data = age_data.describe(\"This is a list of people's ages. Let's keep it private!\")\n",
    "\n",
    "# Let's UPLOAD THE DATA to the Duet server!\n",
    "age_data_pointer = age_data.send(client, searchable=True) # Search = True makes the pointer \"visible\" to everyone\n",
    "print(\"Dataset Pointer Result: \", age_data_pointer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 -  Check the automatic request handlers list\n",
    "As we can see here, our auto request handler queue is empty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.requests.handlers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 - Add a new handler allowing data access\n",
    "Let's create a new handler rule to allow age-data access automatically"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accept_handler = {\n",
    "    \"request_name\": \"age_data\",\n",
    "    \"timeout_secs\": -1,\n",
    "    \"action\": \"accept\", # Accept / Deny\n",
    "    \"print_local\": True, # Print it in Domain Server\n",
    "    \"log_local\": True # Log it in Domain Server\n",
    "}\n",
    "client.requests.add_handler(accept_handler)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6 -  Check the automatic request handlers list again\n",
    "As we can see here, our auto request handler queue is not empty anymore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.requests.handlers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ___PS: We're still able to allow/deny data access requests manually, this feature just aims to show how to manage data access automatically (in a context with multiple data access requests, do it using manual approach wouldn't be feasible)___"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
